<template>
  <div class="box">
    <el-card shadow="never" class="userSearch">
      <span>用户名： </span>
      <el-autocomplete size="small" class="inlineInput" v-model="input" :trigger-on-focus="false"
        :fetch-suggestions="querySearch" placeholder="请输入用户名" @select="handleSelect"
        value-key="username"></el-autocomplete>
      <el-button type="search" size="small" @click="search">查询</el-button>
    </el-card>
    <el-dialog width="30%" title="修改权限" :visible.sync="innerVisible">
      <el-radio v-model="radio" :label="2">管理员</el-radio>
      <el-radio v-model="radio" :label="1">专家</el-radio>
      <el-radio v-model="radio" :label="0">普通用户</el-radio>
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerVisible = false">取 消</el-button>
        <el-button type="primary" @click="empower">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="用户信息" :visible.sync="dialogVisible" width="50%">
      <el-descriptions class="margin-top" :column="2" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-coordinate"></i>
            用户ID
          </template>
          {{ showObj.id }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-user"></i>
            用户名
          </template>
          {{ showObj.username }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            手机号
          </template>
          {{ showObj.phone }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-tickets"></i>
            真实姓名
          </template>
          {{ showObj.realName }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-set-up"></i>
            用户权限
          </template>
          {{ showObj.status == 2 ? '管理员' : '普通用户' }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-office-building"></i>
            创建时间
          </template>
          {{ new Date(showObj.gmtCreate).toLocaleDateString() }}
        </el-descriptions-item>
        <el-descriptions-item>

          <template slot="label">
            <i class="el-icon-edit"></i>
            修改时间
          </template>
          {{ new Date(showObj.gmtModified).toLocaleDateString() }}
        </el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateInfo(showObj)">修改</el-button>
        <el-button type="danger" @click="deleteUser">删除</el-button>
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <el-card shadow="never" class="usersTable">
      <el-table :data="showList" min-height="400" border style="width: 100%; cursor: pointer">
        <el-table-column header-align="center" align="center" prop="username" label="用户名">
        </el-table-column>
        <el-table-column header-align="center" align="center" prop="realName" label="真实姓名">
        </el-table-column>
        <el-table-column header-align="center" align="center" prop="phone" label="电话号码"></el-table-column>
        <el-table-column header-align="center" align="center" label="权限" prop="status">
          <template slot-scope="scope">
            <span v-show="scope.row.status == 0">普通用户</span>
            <span v-show="scope.row.status == 1">专家</span>
            <span v-show="scope.row.status == 2">管理员</span>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click.native.prevent="selectById(scope.row.id)" type="text" size="small" style="color: orange">
              查看
            </el-button>
            <el-button @click.native.prevent="updateInfo(scope.row)" type="text" size="small" style="color: orange"> 修改
            </el-button>
            <el-button @click.native.prevent="deleteRow(scope.row)" type="text" size="small" style="color: red"> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      input: '',
      usersList: [],
      showList: [],
      showObj: {},
      dialogVisible: false,
      innerVisible: false,
      results: [],
      radio: 0,
      updateID: '',
      auth:''
    }
  },
  methods: {
    async getUsers() {
      const { data: res } = await this.$http.post('/web/user/getAllUser')
      if (res.code === 0) {
        this.usersList = this.showList = res.data.user
        if(this.showList.status == 0){
          this.auth = '普通用户'
        }else if(this.showList.status == 1){
          this.auth = '专家'
        }else{
          this.auth = '管理员'
        }
      } else {
        console.log(res.message)
      }
    },
    querySearch(queryString, cb) {
      // console.log(queryString)
      var usersList = this.usersList
      var results = queryString ? usersList.filter(this.createFilter(queryString)) : usersList
      this.results = results
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (usersList) => {
        return (usersList.username.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    handleSelect(item) {
      // console.log(this.input)
      var usersList = this.usersList
      this.results = this.input ? usersList.filter(this.createFilter(this.input)) : usersList
      this.toDetail(item)
    },
    search() {
      if (this.input !== '') {
        // const searchResult = this.usersList.filter(i => i.username === this.input)
        // if (searchResult.length !== 0) {
        //   // console.log(searchResult)
        //   const userId = searchResult[0].id
        //   this.selectById(userId)
        // } else {
        //   this.$message.error('查无该用户')
        // }
        this.showList = this.results
      } else {
        this.showList = this.usersList
      }
    },
    // 根据id查询
    async selectById(id) {
      const { data: res } = await this.$http.get(`/web/user/selectById/${id}`)
      if (res.code === 0) {
        // console.log(res.data.user)
        this.toDetail(res.data.user)
      } else {
        console.log(res.message)
      }
    },
    // 跳转详情页
    toDetail(item) {
      this.showObj = item
      // console.log('跳转', this.showObj)
      this.dialogVisible = true
    },
    deleteUser() {
      this.$confirm('是否确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.dialogVisible = false
        const { data: res } = await this.$http.post(`/web/user/delete/${this.showObj.id}`)
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.getUsers()
        } else {
          console.log(res.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updateInfo(row) {
      // console.log('点击编辑', row.status)
      this.innerVisible = true
      this.updateID = row.id
      console.log(row)
      if (row.status === 2) {
        this.radio = row.status
      } else if(row.status === 1){
        this.radio = row.status
      }else {
        this.radio = 0
      }
    },
    deleteRow(row) {
      console.log('点击删除')
      this.showObj.id = row.id
      this.deleteUser()
    },
    async empower() {
      this.innerVisible = false
      this.dialogVisible = false
      // console.log('赋权', this.updateID, this.radio)
      const { data: res } = await this.$http.post(`/web/user/power/${this.updateID}/${this.radio}`)
      console.log(res)
      if (res.code === 0) {
        this.$message.success('修改成功')
        this.getUsers()
      } else {
        this.$message.error('修改失败')
      }
    }
  },
  mounted() {
    this.getUsers()
  }
}
</script>

<style scoped>
.userSearch {
  width: 100%;
  height: 100%;
  background-color: #fff;
}

.el-button--search {
  background: rgb(8, 46, 83);
  color: #fff;
}

.inlineInput {
  width: 500px;
}

.usersTable {
  width: 100%;
  background-color: #fff;
  margin-top: 20px;
}
</style>
